UserProvider = function(db) {
  var base = this;
  db.collection("users", function (err, users) {
    base.findAll = function(callback) {
      users.find().toArray(function(err, data) {
        callback(err, data);
      });
    }

    base.findById = function(id,callback) {
      users.find({_id:new ObjectID(id)}).toArray(function(err, data) {
        callback(err, data);
      });
    }

    base.insert = function(userid, passwd, callback) {
      doc = { _id:userid, passwd:passwd};
      users.insert(doc, {safe:true}, function(err, data) {
        callback(err, data);
      });
    }

    base.authenticate = function(userid, passwd, callback) {
      doc = {_id:userid, passwd:passwd};
      users.findOne(doc, function(err, data) {
        callback(err, data);
      });
    }
  });
}

exports.UserProvider = UserProvider;
